clear
set type double
set more 1

***************************************
*NOTES
*estimation at the month-firm-destination
*all rhs variables are averaged across the overlapping sample period 2017-2019
*********************************************************************

capture log using log/Table6_erpt_predicted.log, replace


*********************************
*START HERE
*predicted values for col 3 of Table 2
************************************
use ../data/xcurrencychoice_estdata, clear
	drop hs4
	gen hs4=substr(cncode,1,4)
	
	reghdfe Ix mphi_xeu lnmemp ms , absorb(cp date, resid) cluster(vat)
	keep if e(sample)
	predict Ix_hat, xbd
	

***************************************************	
**************************************
*table4: vehicle currency choice
******************************************
preserve
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
drop date *I
	rename country cty_code
	drop if missing(valueX)
    	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueX value

merge m:1 cty_code using ../rawdata/iso_cty_and_currency, keep(match master) nogen
replace cty_name="User" if cty_code=="QW"

	drop if currency=="EUR"
	drop if cty_code=="US"
	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
	drop if usd_peg==1
	drop if missing(currency_code)
	gen Ius=0 if currency==currency_code
	replace Ius=1 if missing(Ius)
	drop if Ius==1 & currency~="USD"
	duplicates tag vat cncode cty_code year month, gen(tag)
	keep if tag==0
	
	keep vat cncode cty_code year month Ius
save temp/vehicle_Ius, replace	

restore
preserve
	merge 1:1 vat cncode cty_code year month using temp/vehicle_Ius, keep(match) nogen
	merge m:1 vat cty_code hs4 using ../data/mcompetitor_xcurrency_dollar, keep(match master) nogen
	

*REGRESSIONS - col 3 table 5
	reghdfe Ius mphi_xeu_euro mphi_xeu_dollar mphi_xeu_nodollar mphi_xeu_missing ms lnmemp, absorb(fe_cp=cp fe_date=date, resid) cluster(vat)
	
	predict Ius_hat, xbd
	gen b_cons=_b[_cons]	
	gen b_mphi_xeu_euro=_b[mphi_xeu_euro]
	gen b_mphi_xeu_dollar=_b[mphi_xeu_dollar]
	gen b_mphi_xeu_nodollar=_b[mphi_xeu_nodollar]
	gen b_mphi_xeu_missing=_b[mphi_xeu_missing]
	gen b_lnmemp=_b[lnmemp]	
	gen b_ms=_b[ms]
		
	gen Ius_hat_test= b_mphi_xeu_euro * mphi_xeu_euro + b_mphi_xeu_dollar * mphi_xeu_dollar + b_mphi_xeu_nodollar * mphi_xeu_nodollar + b_mphi_xeu_missing *mphi_xeu_missing + b_lnmemp * lnmemp + b_ms * ms  + fe_cp + fe_date + 	b_cons
	sum Ius_hat*
	
keep vat cncode cty_code year month hs4 Ius_hat b_* fe_* 
save temp/temp, replace
	keep vat cncode cty_code year month hs4 Ius_hat 
	save temp/Ius_hat, replace
use temp/temp, clear
	 keep b_* 
	 duplicates drop
	 gen n=1
save temp/b_hat, replace

use temp/temp, clear
	keep hs4 cty_code fe_cp
	duplicates drop
save temp/cp_hat, replace
	collapse (mean) fe_p=fe_cp, by(hs4)
save temp/p_hat, replace	

use temp/temp, clear
	keep year month fe_date
	duplicates drop
	drop if missing(fe_date)
save temp/date_hat, replace

restore
	merge 1:1 vat cncode cty_code year month using temp/Ius_hat, keep(match master) nogen
	merge m:1 hs4 cty_code using temp/cp_hat, keep(match master) nogen
	merge m:1 year month using temp/date_hat, keep(match master) nogen
	merge m:1 hs4 using temp/p_hat, keep(match master) nogen
	gen n=1
	merge m:1 n using temp/b_hat, keep(match master) nogen
	

*fill in missing Ius_hat
	gen Ius_fill= b_mphi_xeu_euro * mphi_xeu_euro + b_mphi_xeu_dollar * mphi_xeu_dollar + b_mphi_xeu_nodollar * mphi_xeu_nodollar + b_mphi_xeu_missing *mphi_xeu_missing + b_lnmemp * lnmemp + b_ms * ms  + fe_cp + fe_date + 	b_cons

sum Ius_fill, d

*add in averaged hs4 from stage 2 for missing countries
	gen Ius_fill2= b_mphi_xeu_euro * mphi_xeu_euro + b_mphi_xeu_dollar * mphi_xeu_dollar + b_mphi_xeu_nodollar * mphi_xeu_nodollar + b_mphi_xeu_missing *mphi_xeu_missing + b_lnmemp * lnmemp + b_ms * ms  + fe_p + fe_date + b_cons	
	
sum Ius_fill2, d


*fill in us hat for missings
*******************************
	replace Ius_hat=Ius_fill if missing(Ius_hat)
	replace Ius_hat=Ius_fill2 if missing(Ius_hat)
	sum Ius_hat, d


*winsorize Ix_hat Ius_hat so it lies between zero and 1
	replace Ix_hat=0 if Ix_hat<0
	replace Ix_hat=1 if Ix_hat>1 & !missing(Ix_hat)
	replace Ius_hat=0 if Ius_hat<0
	replace Ius_hat=1 if Ius_hat>1 & !missing(Ius_hat)
	
	gen iota_hat=Ix_hat
	gen iota_us_hat=Ix_hat*Ius_hat
	
*collapse to time-invariant for annual regs
	collapse (mean) iota* , by (vat cncode cty_code)
			
save temp/iota_hat, replace	



**********************************************
*erpt table 6
*********************************************
use ../data/erpt_annual_estdata, clear
merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	
	drop if vehicle_other==1

	drop hs4
	gen hs4=substr(cncode,1,4)
	merge m:1 vat cncode cty_code using temp/iota_hat, keep(match master)
	tab _merge
	drop _merge

eststo clear
*with predicted iota
	gen dxE_iota=dxE*iota_hat 
	gen dxD_iota=dxD*iota_us_hat

*col1	
	*just predicted iota
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_iota dxD_phi dxD_lnmemp dxD_iota lnmemp mphi_xeu iota_hat iota_us_hat , cluster(cy) absorb(cp year)

*col2		
	*predicted iota and actual
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxE_iota dxD_phi dxD_lnmemp dxD_share_usd dxD_iota lnmemp mphi_xeu share_other share_usd iota_hat iota_us_hat , cluster(cy) absorb(cp year)
	
	
	
************************************
*ii. with competitor currency
************************************

	merge m:1 vat cty_code hs4 using ../data/mcompetitor_xcurrency_dollar, keep(match master) nogen
	merge m:1 vat cty_code hs4 using ../data/mcompetitor_xcurrency, keep(match master) nogen

	gen dxE_comp=dxE* mshcompetitor_other
	gen dxD_comp=dxD* mshcompetitor_dollar

*col3	
	*just competitor variables
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_comp dxD_comp dxD_phi dxD_lnmemp lnmemp mphi_xeu mshcompetitor_other mshcompetitor_dollar , cluster(cy) absorb(cp year)

*col4	
	*competitor and actual
	eststo: reghdfe duv_k dxE_comp dxD_comp dxE dxE_phi dxE_lnmemp dxE_share_other dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other mshcompetitor_other mshcompetitor_dollar , cluster(cy) absorb(cp year)
	


*iii. ez_exportsh
****************************************
*exclusion test for ez_exshare	

merge m:1 vat using ../data/Additional_Variables, keep(match master) nogen
	gen dxE_ez_exportsh=dxE*ez_exportsh
	gen dxD_ez_exportsh=dxD*ez_exportsh



*col 5
*instead of currency variable	
	eststo: reghdfe duv_k dxE_ez_exportsh dxD_ez_exportsh ez_exportsh dxE dxE_phi dxE_lnmemp dxD_phi dxD_lnmemp lnmemp mphi_xeu , cluster(cy) absorb(cp year)

*col 6
*in addition to currency variable
	eststo: reghdfe duv_k dxE_ez_exportsh dxD_ez_exportsh ez_exportsh dxE dxE_phi dxE_lnmemp dxE_share_other dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other , cluster(cy) absorb(cp year)

	
	
esttab using "../results/Table6_erpt_predicted.csv",  order(dxE dxE_phi dxD_phi dxE_lnmemp dxD_lnmemp dxE_share_other dxD_share_usd lnmemp mphi_xeu share_other share_usd) se  b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01)  compress replace
eststo clear


log close	
	
	
	
	
	


